package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.ExamPaper;
import com.jyy.prefabricated.enums.BaseStatusEnum;
import com.jyy.prefabricated.enums.DeletedEnum;
import com.jyy.prefabricated.enums.PaperEnum;
import com.jyy.prefabricated.service.ContentCategoryService;
import com.jyy.prefabricated.service.ExamPaperService;
import com.jyy.prefabricated.vo.ContentCategoryData;
import com.jyy.prefabricated.vo.PracticeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 练习管理
 *
 * @version 1.0.0
 * @author: duanjianhui
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class PracticeController {

    @Autowired
    private ExamPaperService paperService;

    @Resource
    private ContentCategoryService contentCategoryService;

    /**
     * 新增练习
     *
     * @param paper
     * @return
     */
    @PostMapping("/practice-paper")
    public AjaxResult insert(@Valid @RequestBody ExamPaper paper) {
        log.info("Rest request to insert exam-paper:{}", paper);
        paper.setStatus(BaseStatusEnum.INVALID.getValue());
        paper.setDeleted(DeletedEnum.INVALID.getValue());
        paper.setPaperStatus(PaperEnum.PRACTICE.getValue());
        int id = paperService.insert(paper);
        ExamPaper result = paperService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 修改练习
     *
     * @param paper
     * @return
     */
    @PutMapping("/practice-paper")
    public AjaxResult update(@Valid @RequestBody ExamPaper paper) {
        log.info("Rest request to update exam-paper:{}", paper);
        paperService.update(paper);
        ExamPaper result = paperService.selectById(paper.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询练习详情
     *
     * @param id
     * @return
     */
    @GetMapping("/practice-paper/{id}")
    public AjaxResult selectById(@PathVariable Integer id) {
        log.info("Rest request to find exam-paper by id:{}", id);
        ExamPaper result = paperService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除练习
     *
     * @param id
     * @return
     */
    @DeleteMapping("/practice-paper/{id}")
    public AjaxResult deleteById(@PathVariable Integer id) {
        log.info("Rest request to delete exam-paper by id:{}", id);
        int row = paperService.delete(id);
        if (row > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 批量删除练习
     *
     * @param dto
     * @return
     */
    @DeleteMapping("/practice-paper-batch")
    public AjaxResult deleteById(@RequestBody BatchDto dto) {
        log.info("Rest request to batch-delete course by id:{}", dto);
        int row = paperService.batchDelete(dto);
        if (row > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询练习列表
     *
     * @param name       试卷名称
     * @param status     状态
     * @param sortStatus 排序状态（1、最新、2、最热、3、免费）
     * @param categoryId 分类id
     * @param pageable   分页信息
     * @return
     */
    @GetMapping("/practice-paper")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String name, @RequestParam(required = false) Integer status, @RequestParam(required = false) Integer sortStatus, @RequestParam(required = false) Integer categoryId, Pageable pageable) {
        log.info("Rest request to find exam-paper  by params name:{},status:{}", name, status);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Integer> categoryIds = new ArrayList<>();
        if (categoryId != null) {
            List<ContentCategoryData> contentCategoryDataList = contentCategoryService.selectTree(categoryId);
            categoryIds = contentCategoryDataList.stream().map(e -> e.getId()).collect(Collectors.toList());
        }
        List<PracticeVo> papers = paperService.selectPracticeByParams(name, status, PaperEnum.PRACTICE.getValue(), categoryIds, sortStatus);
        PageInfo<PracticeVo> result = new PageInfo<>(papers);
        return AjaxResult.success(result);
    }

//    /**
//     * 分页查询练习列表
//     *
//     * @param  试卷名称
//     * @param    状态
//     * @param  分页信息
//     * @return
//     */
//    @GetMapping("/practice-paper-mobile")
//    public AjaxResult selectByParams() {
//        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
//        List<PracticeVo> papers = paperService.selectPracticeByParams(name, status, PaperEnum.PRACTICE.getValue());
//        PageInfo<PracticeVo> result = new PageInfo<>(papers);
//        return AjaxResult.success(result);
//    }

    /**
     * 查询练习列表
     *
     * @param name   试卷名称
     * @param status 状态
     * @return
     */
    @GetMapping("/practice-paper-list")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String name, @RequestParam(required = false) Integer status) {
        log.info("Rest request to find exam-paper  by params name:{},status:{}", name, status);
        List<PracticeVo> result = paperService.selectByParams(name, status, PaperEnum.EXAM.getValue(),null);
        return AjaxResult.success(result);
    }

    /**
     * 批量更新发布状态
     * @param dto
     * @return
     */
    @PutMapping("/practice-paper-batch")
    public AjaxResult update(@Valid @RequestBody BatchDto dto){
        log.info("Rest request to batch-update course:{}",dto);
        paperService.batchUpdateStatus(dto);
        return AjaxResult.success();
    }
}
